var buildTableGrid = function(table) {
	var tDims = table.dimensions;
	var tData = table.data;
	var tHdrs = table.headers;

	var colsNb = tDims[tDims.length - 1] + (tHdrs[tHdrs.length - 2]).length;
	var gridData = [];
	for (var i = 0; i < tDims[tDims.length - 2]; i++) {
		gridData[i] = [];
		for (var j = 0; j < (tHdrs[tHdrs.length - 2]).length; j++) {
			gridData[i].push((tHdrs[tHdrs.length - 2])[j][i]);
		}
		for (var j = 0; j < tData[i].length; j++) {
			gridData[i].push(tData[i][j]);
		}
	}

	var hdrLinesNb = (tHdrs[tHdrs.length - 1]).length;
	var rowsNb = tDims[tDims.length - 2] + hdrLinesNb;
	var gridFields = [];
	for (var i = 0; i < colsNb; i++) {
		gridFields[i] = {};
		if (i < (tHdrs[tHdrs.length - 2]).length) {
			gridFields[i].name = ('rh_' + i);
		} else {
			gridFields[i].name = ('col_' + (i - (tHdrs[tHdrs.length - 2]).length));
		}
	}

	var gridStore = new Ext.data.ArrayStore({
				fields : gridFields,
				data : gridData
			});

	var gridCols = [];
	for (var i = 0; i < colsNb; i++) {
		gridCols[i] = {};
		if (i < (tHdrs[tHdrs.length - 2]).length) {
			gridCols[i].text = '';
			gridCols[i].flex = 1;
			gridCols[i].sortable = false;
			gridCols[i].dataIndex = ('rh_' + i);
		} else {
			var colHdrs = (tHdrs[tHdrs.length - 1]);
			if (hdrLinesNb == 1) {
				gridCols[i].text = colHdrs[colHdrs.length - 1][i
						- (tHdrs[tHdrs.length - 2]).length];
				gridCols[i].sortable = false;
				gridCols[i].dataIndex = ('col_' + (i - (tHdrs[tHdrs.length - 2]).length));
			} else {
				var currentCols = gridCols[i];
				for (var j = 0; j < hdrLinesNb; j++) {
					currentCols.text = colHdrs[j][i
							- (tHdrs[tHdrs.length - 2]).length];
					if (j < hdrLinesNb - 1) {
						currentCols.columns = [];
						var subCol = {};
						currentCols.columns.push(subCol);
						currentCols = subCol;
					} else {
						currentCols.sortable = false;
						currentCols.dataIndex = ('col_' + (i - (tHdrs[tHdrs.length
								- 2]).length));
					}
				}
			}
		}
	}

	var w = colsNb * 102 < Ext.getCmp('allTabs').getWidth()
			? colsNb * 102
			: '100%';
	var h = rowsNb * 30 < Ext.getCmp('allTabs').getHeight()
			? rowsNb * 30
			: '100%';
	var s = true;
	if (colsNb * 102 >= Ext.getCmp('allTabs').getWidth()
			&& rowsNb * 30 < Ext.getCmp('allTabs').getHeight())
		s = 'horizontal';
	else if (rowsNb * 30 >= Ext.getCmp('allTabs').getHeight()
			&& colsNb * 102 < Ext.getCmp('allTabs').getWidth())
		s = 'vertical';

	var as_tableGrid = Ext.create('Ext.grid.Panel', {
				id : 'as_tableGrid',
				title : 'Analysis Table',
				store : gridStore,
				columnLines : true,
				columns : gridCols,
				height : h,
				width : w,
				scroll : s,
				viewConfig : {
					stripeRows : true
				}
			});

	return as_tableGrid;
};

// for chart test
var buildGraphicChart = function(table) {
	var tDims = table.dimensions;
	var tData = table.data;
	var tHdrs = table.headers;

	var chartFields = [];
	chartFields[0] = 'name';
	chartFields[1] = 'data';

	var chartData = [];
	var dimHdr = tHdrs[tHdrs.length - 2];
	for (var i = 0; i < tDims[tDims.length - 2]; i++) {
		chartData[i] = {};
		chartData[i].name = '';
		for (var j = 0; j < dimHdr.length; j++) {
			chartData[i].name += dimHdr[j][i];
			if (j < dimHdr.length - 1)
				chartData[i].name += ' - ';
		}
		chartData[i].data = tData[i].avg();
	}

	var chartStore = new Ext.data.JsonStore({
				fields : chartFields,
				data : chartData
			});

	return Ext.create('Ext.chart.Chart', {
				id : 'as_graphicChart',
				width : Ext.getCmp('allTabs').getWidth() - 50,
				height : Ext.getCmp('allTabs').getHeight() - 50,
				animate : true,
				shadow : false,
				store : chartStore,
				axes : [{
							type : 'Numeric',
							grid : true,
							position : 'bottom',
							fields : ['data'],
							title : 'Average Value',
							grid : {
								odd : {
									opacity : 1,
									fill : '#ddd',
									stroke : '#bbb',
									'stroke-width' : 1
								}
							},
							minimum : 0,
							adjustMinimumByMajorUnit : 0
						}, {
							type : 'Category',
							position : 'left',
							fields : ['name'],
							title : 'Category',
							grid : true,
							label : {
								rotate : {
									degrees : 315
								}
							}
						}],
				series : [{
					type : 'bar',
					highlight : true,
					axis : 'bottom',
					xField : 'name',
					yField : ['data'],
					tips : {
						trackMouse : true,
						width : 200,
						height : 40,
						renderer : function(storeItem, item) {
							this.setTitle(storeItem.get('name') + ' : '
									+ storeItem.get('data'));
						}
					},
					label : {
						display : 'insideEnd',
						field : 'data',
						renderer : Ext.util.Format.numberRenderer('0'),
						orientation : 'horizontal',
						color : '#333',
						'text-anchor' : 'middle',
						contrast : true
					},
					style : {
						opacity : 0.9
					}
				}]
			});
}